﻿@inject IDataCacheService _dataCacheService
@inject IPopupService PopupService


<MDialog @bind-Value="Dialog"
         Width="600">
    <MDCard>
        <MCardTitle Class="text-h5">
            编辑目标
        </MCardTitle>
        <MCardText>
            达到相应目标会在页面展示
        </MCardText>
        <MCardText Class="pb-0">
            <div class="row">
                <div class="col-12  col-sm-6">
                    <MTextField @bind-Value="Model.Target" Label="目标人数" Type="number" Autofocus/>
                </div>
                <div class="col-12  col-sm-6">
                    <MTextField @bind-Value="Model.Name" Label="名称" OnKeyDown="OnKeyDown" />
                </div>
            </div>
        </MCardText>
        <MCardActions Class="pb-6 pe-6 ps-6">
            <MSpacer></MSpacer>
            <CnGalWebSite.Components.Buttons.MasaButton Text="保存" IsAsync="true" OnClick="OnSave" Icon="mdi-check" Color="success" TextStyle />
        </MCardActions>

    </MDCard>
</MDialog>
@code {

    private EditBookingGoalModel Model { get; set; }
    [Parameter]
    public List<EditBookingGoalModel> Items { get; set; }
    [Parameter]
    public EventCallback OnChanged { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    bool Dialog { get; set; }

    private async Task OnKeyDown(KeyboardEventArgs e)
    {
        if (e.Code == "Enter" || e.Code == "NumpadEnter")
        {
            await OnSave();
        }
    }

    public void Refresh(EditBookingGoalModel model)
    {
        Model = model;
        Dialog = true;

        StateHasChanged();

    }

    public async Task OnSave()
    {
        if (Items.Count(s => s.Name == Model.Name)>1)
        {
            await PopupService.ToastAsync("名称冲突", AlertTypes.Error);
            return;
        }

        Dialog = false;
        StateHasChanged();
        await OnChanged.InvokeAsync();
    }

}
